package com.ea.admin.service.impl;

import com.ea.admin.mapper.AdminAccountMapper;
import com.ea.admin.mapper.DeptInfoMapper;
import com.ea.admin.pojo.Enum.RoleCode;
import com.ea.admin.service.CompanyPermissionService;
import com.ea.admin.service.DeptPermissionService;
import com.ea.admin.service.PermissionService;
import com.ea.admin.service.PositionService;
import com.ea.common.result.BizException;
import com.ea.common.result.ErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    CompanyPermissionService companyPermissionService;
    @Resource
    DeptPermissionService deptPermissionService;
    @Resource
    PositionService positionService;
    @Resource
    AdminAccountMapper adminAccountMapper;
    @Resource
    DeptInfoMapper deptInfoMapper;

    @Override
    public PermissionList checkAndRefreshPermissionList(List<Long> companyIdList, List<Long> deptIdList, Long accountId) {
        return checkAndRefreshPermissionList(companyIdList, deptIdList, null, accountId);
    }

    @Override
    public PermissionList checkAndRefreshPermissionList(List<Long> companyIdList, List<Long> deptIdList, List<Long> positionIdList, Long accountId) {
        Long adminRoleId = adminAccountMapper.selectById(accountId).getAdminRoleId();
        List<Long> allCompanyIdList = companyPermissionService.getCompanyIdList(accountId);
        List<Long> allPositionIdList = positionService.getPositionIdList(accountId);
        List<Long> allDeptIdList;

        // 单位管理员以上层级校验
        if(adminRoleId== RoleCode.COMPANY_SUPER_ADMIN.getCode().longValue()||
                adminRoleId== RoleCode.COMPANY_ADMIN.getCode().longValue()) {

            // 单位层级管理员获取所有部门权限
            allDeptIdList=deptInfoMapper.getAllByCompanyId(allCompanyIdList);

            if (allCompanyIdList == null || allCompanyIdList.isEmpty()) {
                throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_EMPTY_ERROR);
            }
            // 如果id中有无权限的单位id，则抛出异常
            if (companyIdList != null && !companyIdList.isEmpty() && !new HashSet<>(allCompanyIdList).containsAll(companyIdList)) {
                throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_ERROR);
            }
            if (allPositionIdList == null || allPositionIdList.isEmpty()) {
                throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_EMPTY_ERROR);
            }
            // 如果id中有无权限的职位id，则抛出异常
            if (positionIdList != null && !positionIdList.isEmpty() && !new HashSet<>(allPositionIdList).containsAll(positionIdList)) {
                throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_ERROR);
            }
        }else {// 部门管理员权限校验

            allDeptIdList = deptPermissionService.getDepartmentIdList(accountId);
            if (allDeptIdList == null || allDeptIdList.isEmpty()) {
                throw new BizException(ErrorCode.USER_DEPT_PERMISSION_EMPTY_ERROR);
            }
            // 如果id中有无权限的部门id，则抛出异常
            if (deptIdList != null && !deptIdList.isEmpty() && !new HashSet<>(allDeptIdList).containsAll(deptIdList)) {
                throw new BizException(ErrorCode.USER_COMPANY_PERMISSION_ERROR);
            }
        }


        PermissionList permissionList = new PermissionList();
        if (companyIdList != null && !companyIdList.isEmpty()) {
            permissionList.companyIdList = companyIdList;
        } else {
            permissionList.companyIdList = allCompanyIdList;
        }
        if (deptIdList != null && !deptIdList.isEmpty()) {
            permissionList.deptIdList = deptIdList;
        } else {
            permissionList.deptIdList = allDeptIdList;
        }
        if (positionIdList != null && !positionIdList.isEmpty()) {
            permissionList.positionIdList = positionIdList;
        } else {
            permissionList.positionIdList = allPositionIdList;
        }
        return permissionList;
    }

    public static class PermissionList {
        List<Long> companyIdList;
        List<Long> deptIdList;
        List<Long> positionIdList;
    }
}
